<template>
  <view>
    <!-- 预留出的搜索区域 -->
    <my-search @click="gotoSearch" />

    <view class="scroll-view-container">
      <!-- 左侧的滚动区域 -->
      <scroll-view
        class="left-scroll-view"
        scroll-y
        :style="{ height: wh + 'px' }"
      >
        <view
          v-for="(cat1, index1) in categoryList"
          :key="index1"
          class="left-scroll-view-item"
          :class="{ active: selectCategory === index1 }"
          @click="changeSelect(index1)"
          >{{ cat1.cat_name }}</view
        >
      </scroll-view>

      <!-- 右侧的滚动区域 -->
      <scroll-view
        class="right-scroll-view"
        scroll-y
        :style="{ height: wh + 'px' }"
        :scroll-top="scrollTop"
      >
        <!-- 动态渲染二级分类的列表数据 -->
        <view
          v-for="(item, index2) in categoryList[selectCategory].children"
          :key="index2"
          class="cate-lv2"
        >
          <view class="cate-lv2-title">/ {{ item.cat_name }} /</view>
          <!-- 动态渲染三级分类的列表数据 -->
          <view class="cate-lv3-list">
            <view
              class="cate-lv3-item"
              v-for="(item2, index3) in item.children"
              :key="index3"
              @click="goProductList(item2)"
            >
              <image :src="item2.cat_icon"></image>
              <text>{{ item2.cat_name }}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
import MySearch from "@/components/my-search.vue";
import badge from "@/mixins/badge.js";
export default {
  mixins: [badge],
  components: {
    MySearch,
  },
  data() {
    return {
      // 可滚动区域的高度
      wh: 0,
      // 分类的数据数组
      categoryList: [],
      // 控制左侧菜单的选中
      selectCategory: 0,
      scrollTop: 0,
    };
  },
  created() {
    // 1. 获取系统信息
    const systemInfo = uni.getSystemInfoSync();
    // console.log(systemInfo);
    // 2. 获取可视区域的高度 = 屏幕的高度 - 导航栏的高度 - tabBar的高度 - 搜索框的高度
    this.wh = systemInfo.windowHeight - 50;

    // 3. 获取分类数据
    this.getCategoryList();
  },
  methods: {
    // 1. 获取分类数据
    async getCategoryList() {
      const { meta, message } = await this.$http.get("/categories");
      // 请求失败处理
      if (meta.status !== 200) {
        return this.$msg();
      }

      this.categoryList = message;
    },
    // 2. 切换左侧菜单
    changeSelect(index) {
      // 2.1 设置左侧选中
      this.selectCategory = index;
      // 2.2 让scrollTop的值为0
      this.scrollTop = this.scrollTop === 0 ? 1 : 0;
    },
    // 3. 去商品列表页
    goProductList(item) {
      // console.log(item);
      uni.navigateTo({
        url: `/pages_sub1/product-list/product-list?cid=${item.cat_id}`,
      });
    },
    // 4. 去搜索页面
    gotoSearch() {
      uni.navigateTo({
        url: "/pages_sub1/search/search",
      });
    },
  },
};
</script>

<style scoped lang="scss">
.scroll-view-container {
  display: flex;

  .left-scroll-view {
    width: 240rpx;

    .left-scroll-view-item {
      line-height: 120rpx;
      background-color: #f7f7f7;
      text-align: center;
      font-size: 24rpx;

      // 激活项的样式
      &.active {
        background-color: #ffffff;
        position: relative;

        // 渲染激活项左侧的红色指示边线
        &::before {
          content: " ";
          display: block;
          width: 6rpx;
          height: 60rpx;
          background-color: #c00000;
          position: absolute;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }
  }
}

.cate-lv2-title {
  font-size: 24rpx;
  font-weight: bold;
  text-align: center;
  padding: 30rpx 0;
}

.cate-lv3-list {
  display: flex;
  flex-wrap: wrap;

  .cate-lv3-item {
    width: 33.33%;
    margin-bottom: 20rpx;
    display: flex;
    flex-direction: column;
    align-items: center;

    image {
      width: 120rpx;
      height: 120rpx;
    }

    text {
      font-size: 24rpx;
    }
  }
}
</style>